const bookFetch = require('./book_fetch');

class BookController extends Controller {
    get bookInfo() {
        return {
            key: this.url,
            title: this.data.title,
            subtitle: this.data.subtitle,
            picture: this.data.picture,
            page: 'book',
            data: {
                link: this.url,
                title: this.data.title,
                subtitle: this.data.subtitle,
                picture: this.data.picture,
                summary: this.data.summary,
            },
        };
    }

    async load(data) {
        this.url = "http://192.168.137.1:8080/manga360/getmangaInfo.do?key="+data.key;
        this.data = {
            title: data.title,
            subtitle: data.subtitle,
            summary: "",
            picture: data.picture,
            loading: false,
            editing: false,
            reverse: localStorage['reverse'] != 'false',
            list: []
        };
            this.reload();
    }

    unload() {

    }

    onRefresh() {
        this.reload();
    }

    async reload() {
        this.setState(()=>{
            this.data.loading = true;
        });
        try {
            let url = this.url;
            let data = await bookFetch(url);
            data=data.data

            let now = new Date().getTime();
            data.time = now;
            this.setState(()=>{
                this.data.title = data.title;
                this.data.list = data.data;
                this.data.loading = false;
            });
        } catch (e) {
            showToast(`${e}\n${e.stack}`);
            this.setState(()=>{
                this.data.loading = false;
            });
        }
    }

    onFavoritePressed() {
        this.setState(()=>{
            if (this.isFavarite()) {
                FavoritesManager.remove(this.url);
            } else {
                let last;

                /**
                 * Add to favorites list
                 *
                 * The first argument see `bookinfo`
                 *
                 * The second argument is optional
                 * @param {String}title The title of the last chapter
                 * @param {String}key The unique identifier of the last chapter
                 */
                if (this.data.list.length > 0) {
                    let data = this.data.list[this.data.list.length - 1];
                    last = {
                        title: data.title,
                        key: data.link,
                    };
                }
                this.addFavorite(this.bookInfo, last);
            }
        });
    }

    onDownloadPressed() {
        this.setState(()=>{
            this.data.editing = true;
        });
    }

    onClearPressed() {
        this.selected = [];
        this.setState(()=>{
            this.data.editing = false;
        });
    }

    onCheckPressed() {
        let downloads = [];
        for (let idx of this.selected) {
            var data = this.data.list[idx];
            /**
             * Add to download queue
             *
             * @param {String}key The unique identifier of the download item
             * @param {String}title The name of book
             * @param {String}subtitle The subtitle of book
             * @param {String}link The url of book, To group items with same book link.
             * @param {String}picture The cover of book.
             * @param {Object}data Data will be sent to processor load function.
             * @param {String*}data.title The title of chapter.
             */
            downloads.push({
                key: data.link,
                title: this.data.title,
                link: this.url,
                picture: this.data.picture,
                subtitle: this.data.subtitle,
                data: data,
            });
        }
        this.addDownload(downloads);

        this.selected = [];
        this.setState(()=>{
            this.data.editing = false;
        });
    }

    async onPressed(idx) {

        this.data.list.push({
            title: "hello01",
            subtitle: "hello02",
            link: "https://res99.shut123.com/image/view/4153072/0.webp",
        });
        this.data.list.push({
            title: "hello01",
            subtitle: "hello02",
            link: "https://res99.shut123.com/image/view/4153072/0.webp",
        });
        this.data.list.push({
            title: "hello01",
            subtitle: "hello02",
            link: "https://res99.shut123.com/image/view/4153072/0.webp",
        });

        await this.openBook({
            // key: this.data.list[idx],
            key:"shanliangdesishen",
            list: this.data.list,
            index: idx,
        });
        this.setState(()=>{
            this.data.last = this.getLast();
        })
    }

    async onLastPressed() {
        let key = this.getLastKey(this.url);
        let idx;
        if (key) {
            for (let i = 0, t = this.data.list.length; i < t; ++i) {
                let data = this.data.list[i];
                if (data.link === key) {
                    idx = i;
                    break;
                }
            }
        }
        if (typeof idx === 'number') {
            await this.openBook({
                key: this.url,
                list: this.data.list,
                index: idx,
            });
            this.setState(()=>{
                this.data.last = this.getLast();
            })
        }
    }

    isSelected(index) {
        return this.selected.indexOf(index) >= 0;
    }

    onSourcePressed() {
        this.openBrowser(this.url);
    }

    onOrderSelected(value) {
        this.setState(()=>{
            this.data.reverse = value;
            localStorage['reverse'] = value.toString();
        });
    }

    isDownloaded(index) {
        let item = this.data.list[index];
        return DownloadManager.exist(item.link);
    }

    isFavarite() {
        return FavoritesManager.exist(this.url);
    }

    getLast() {
        if (this.getLastKey) {
            let key = this.getLastKey(this.url);
            if (key) {
                for (let data of this.data.list) {
                    if (data.link === key) {
                        var title = data.title;
                        if (title.length > 18) {
                            title = '...' + title.substr(title.length - 16)
                        }
                        return title;
                    }
                }
            }
        }
        return null
    }
}

module.exports = BookController;